package text.adventure.ui;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;


import text.adventure.input.CSVDictReader;
import text.adventure.input.Parser;
import text.adventure.input.SynFilter;
import text.adventure.input.SynHandler;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TextadventureActivity extends Activity implements OnClickListener {
	
	// Dummy Implementation
	TextView tv;
	Parser p;
	EditText input;
	
    /** Called when the activity is first created. */
    @Override 
    public void onCreate(Bundle savedInstanceState) {
    	//Parser
		CSVDictReader reader = new CSVDictReader();
		List<String> verbData = reader.convertCSVtoList("verbs.csv", ";", this);
		List<String> nounData = reader.convertCSVtoList("nouns.csv", ";", this);
		List<String> otherData = reader.convertCSVtoList("other.csv", ";", this);
		p = new Parser(nounData, verbData, otherData);
    	
		//XML Synonyms
		Map<String, String> synonyms = new HashMap<String, String>();
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			SynHandler sh = new SynHandler();
			xr.setContentHandler(sh);
			xr.parse(new InputSource(getAssets().open("xml/synonyms.xml")));
			synonyms = sh.getResultMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
		p.setSynFilter(new SynFilter(synonyms));
    	
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        
        tv = new TextView(this);
        tv.setGravity(Gravity.TOP);
        tv.setTextSize(21);
        tv.setText("Some stuff happened. Probably with zombies.");
        
        input = new EditText(this);
        input.setGravity(Gravity.CENTER);
        
        Button button = new Button(this);
        button.setText("Do it");
        button.setOnClickListener(this);
        
        layout.addView(tv);
        layout.addView(input);
        layout.addView(button);
        setContentView(layout);
        
    }

	@Override
	public void onClick(View v) {
		if (p.processInput(input.getText().toString())) {
			tv.setText("Valid. Still zombies though." + " " + p.testReturn.getVerb() + " " + p.testReturn.getObject(0) );
		} else {
			tv.setText("Invalid. You suck at this.");
		}
		
		
	}
}